/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.zhixun.mobile.common.utils;

import java.io.UnsupportedEncodingException;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.thinkgem.jeesite.common.utils.DateUtils;

/**
 * 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
 * @author ThinkGem
 * @version 2013-01-15
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
	private static final String CHARSET_NAME = "UTF-8";
	/**
	 * 替换掉HTML标签
	 */
	public static String replaceHtml(String html) {
		String regEx = "<.+?>";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(html);
		String s = m.replaceAll("");
		return s;
	}
	/**
     * 转换为字节数组
     * @param str
     * @return
     */
    public static String toString(byte[] bytes){
    	try {
			return new String(bytes, CHARSET_NAME);
		} catch (UnsupportedEncodingException e) {
			return EMPTY;
		}
    }
    /**
     * 转换为字节数组
     * @param str
     * @return
     */
    public static byte[] getBytes(String str){
    	if (str != null){
    		try {
				return str.getBytes(CHARSET_NAME);
			} catch (UnsupportedEncodingException e) {
				return null;
			}
    	}else{
    		return null;
    	}
    }
	/**
	 * 缩略字符串（不区分中英文字符）*
	 * @param str 目标字符串
	 * @param length 截取长度
	 * @return
	 */
	public static String abbr(String str, int length) {
		if (str == null) {
			return "";
		}
		try {
			StringBuilder sb = new StringBuilder();
			int currentLength = 0;
			for (char c : str.toCharArray()) {
				currentLength += String.valueOf(c).getBytes("GBK").length;
				if (currentLength <= length - 3) {
					sb.append(c);
				} else {
					sb.append("...");
					break;
				}
			}
			return sb.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";

	}
	
	/**判断str1中是否 有 数字 在 str2中出现
	 * @param str1	"1,2,3,"
	 * @param str2	"1,4,5,"
	 * @return
	 */
	public static boolean containss(String str1,String str2){
		String str[]=str1.split(",");
		for(String s:str){
			if(str2.contains(s+",")){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 生成随机的小写字母
	 */
	public static String randomLetter(){
		String letter="";
		letter= letter+(char) (Math.random ()*26+'a');
		
		
		return letter;
	}
	
	
	/**
	 * 判定复选框是否选中
	 * @param categoryId
	 * @param serviceCategoryIds
	 * @return
	 */
	public static String isCheckboxChecked(String categoryId,String serviceCategoryIds) {
		String checked = "";
		if(serviceCategoryIds != null && !serviceCategoryIds.equals("")){
			String[] categoryIds = serviceCategoryIds.split(",");
			for (String id : categoryIds) {
				if(id.equals(categoryId)){
					checked = "checked";
					break;
				}
			}
		}
		return checked;
	}
	
	
	/**获取字符 第几次出现的位置
	 * @author hjt
	 * @param str
	 * @param index
	 * @return
	 * 2016-11-22
	 */
	public static int getStrIndex(String str,char ch,int index){
		
		int number=0;
		for (int j = 0; j < str.length(); j++) {
			if (str.charAt(j)==ch) {
				number++;
			}
			
			if (number == index) {
				return j;
			}
		}
		return number;
	}
	
	public static Long getMinutestr(String strOrderTime){
		if(StringUtils.isEmpty(strOrderTime)){
			return 100l;
		}
		String strOrderTimeList[]=strOrderTime.split(",");
		Date orderDate=DateUtils.getDateBystr(strOrderTimeList[strOrderTimeList.length-1],"yyyy-MM-dd HH:mm:ss");
		Long mc = DateUtils.getMinute(new Date(),orderDate);
		return mc;
	}

}

